<template>
  <el-dialog v-model="showDialog" title="新增团队" custom-class="teamDialog">
    <el-form
      ref="ruleFormRef"
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      :rules="rules"
    >
      <el-form-item label="上级部门" prop="prevDepId">
        <el-cascader
          ref="cascaderRef"
          :show-all-levels="false"
          v-model="formInline.prevDepId"
          :options="prevDepOptions"
          :placeholder="'请选择上级部门'"
          :props="cascaderProps"
          @change="handleChange"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="depName">
        <el-input v-model="formInline.depName" placeholder="请输入部门名称" clearable />
      </el-form-item>
      <el-form-item label="负责人" placeholder="请输入负责人名称">
        <el-input
          :disabled="true"
          v-model="formInline.employeeName"
          placeholder="请输入负责人名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="联系方式">
        <el-input
          :disabled="true"
          v-model="formInline.employeePhone"
          placeholder="请输入负责人联系方式"
          clearable
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formInline.state">
          <el-radio label="0">正常</el-radio>
          <el-radio label="1">异常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref, computed, reactive } from 'vue';
import CommonSelect from '@/components/CommonSelect.vue';

const emit = defineEmits(['update:addDepDialog', 'handleConfirm']);

const props = defineProps({
  addDepDialog: {
    type: Boolean,
    default: false,
  },
  prevDepOptions: {
    type: Array,
    default: [],
  },
});

const cascaderRef = ref(null);
const ruleFormRef = ref(null);

const showDialog = computed({
  get() {
    return props.addDepDialog;
  },
  set(v) {
    emit('update:addDepDialog', v);
  },
});

const formInline = reactive({
  prevDepId: '',
  depName: '', //部门名称
  employeeName: '', //负责人名称
  employeePhone: '', //负责人手机号
  employeeEmail: '', //负责人邮箱
  employeeId: '',
  teamStatus: '',
  state: '0',
});

const rules = reactive({
  prevDepId: [
    {
      required: true,
      message: '请选择上级部门',
      trigger: 'blur',
    },
  ],
  depName: [
    {
      required: true,
      message: '请输入部门名称',
      trigger: 'blur',
    },
  ],
});

const cascaderProps = {
  emitPath: false,
  checkStrictly: true,
};
const handleChange = (v) => {
  const node = cascaderRef.value.getCheckedNodes(false);
  formInline.employeeId = node[0].data.employeeId;
  formInline.employeeName = node[0].data.employeeName;
  formInline.employeePhone = node[0].data.employeePhone;
};

const handleConfirm = () => {
  emit('handleConfirm', formInline);
  showDialog.value = false;
};

const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      emit('handleConfirm', formInline);
      showDialog.value = false;
    } else {
      console.log('error submit!', fields);
    }
  });
};
</script>

<style lang="less">
.teamDialog {
  width: 35vw;

  .el-dialog__body {
    .el-form {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;

      .el-form-item {
        width: 40%;
      }
    }
  }
}
</style>